package net.butfly.bus.client.connection.impl;

import java.net.MalformedURLException;
import java.util.List;
import java.util.Map;

import net.butfly.bus.EntryPoint;
import net.butfly.bus.Request;
import net.butfly.bus.Response;
import net.butfly.bus.client.connection.Connection;
import net.butfly.bus.context.Constants;
import net.butfly.bus.exception.BusRuntimeException;
import net.butfly.bus.util.EnumFactory;
import net.butfly.bus.util.LogUtils;

import com.caucho.hessian.client.HessianConnectionException;
import com.caucho.hessian.client.HessianProxyFactory;
import com.caucho.hessian.client.HessianRuntimeException;
import com.caucho.hessian.io.AbstractSerializerFactory;

public class HessianConnection implements Connection {
	private static LogUtils logger = LogUtils.getLog();

	private String path;

	private long connectionTimeout = 5000;

	// private List<Map<String, String>> translator;

	public HessianConnection(String path, long timeout, List<Map<String, String>> translator) {
		this.path = path;
		this.connectionTimeout = timeout > 0 ? timeout : connectionTimeout;
		// translator = translator;
	}

	public Response send(Request request) {
		try {
			HessianProxyFactory factory = new HessianProxyFactory();

			AbstractSerializerFactory af = null;
			try {
				af = (AbstractSerializerFactory) Thread.currentThread().getContextClassLoader()
						.loadClass(Constants.EXT_EBASE_ENUMFACTORY).newInstance();
			} catch (Exception ex) {
				af = new EnumFactory();
			}
			factory.getSerializerFactory().addFactory(af);
			factory.setConnectTimeout(connectionTimeout);

			EntryPoint proxy = (EntryPoint) factory.create(EntryPoint.class, path);
			logDebug("尝试连接远程Hessian应用 " + path + ".");
			return (Response) proxy.invoke(request);
		} catch (MalformedURLException ex) {
			throw new BusRuntimeException(Constants.SystemError.HESSIAN_CONNECTION, "连接地址 " + path
					+ " 地址配置异常,请检查该服务器是否运行正常.", ex);
		} catch (HessianConnectionException ex) {// 连接异常
			throw new BusRuntimeException(Constants.SystemError.HESSIAN_CONNECTION,
					"连接 " + path + " 异常,请检查该服务器是否运行正常.", ex);
		} catch (HessianRuntimeException ex) {
			throw new BusRuntimeException(Constants.SystemError.HESSIAN_CONNECTION,
					"连接 " + path + " 异常,请检查该服务器是否运行正常.", ex.getCause());
		}
	}

	// --------------------------------------------------------------------
	private static void logDebug(String message) {
		if (logger.isDebugEnabled()) {
			logger.debug(LogUtils.getInstance().getFlowNo() + message);
		}
	}

	public static class HessianConfigBean {
		private String path;
		private String timeout;
		private List<Map<String, String>> typeTranslator;

		public String getPath() {
			return path;
		}

		public void setPath(String path) {
			this.path = path;
		}

		public List<Map<String, String>> getTypeTranslator() {
			return typeTranslator;
		}

		public void setTypeTranslator(List<Map<String, String>> serializers) {
			this.typeTranslator = serializers;
		}

		public String getTimeout() {
			return timeout;
		}

		public void setTimeout(String connectionTimeout) {
			this.timeout = connectionTimeout;
		}

	}
}
